package com.clever.mall.service.impl;

import com.clever.mall.commons.dto.AddressDto;
import com.clever.mall.commons.exception.ServiceException;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.entity.Address;
import com.clever.mall.mapper.AddressMapper;
import com.clever.mall.service.AddressService;
import com.clever.mall.utils.IsClassNull;
import com.clever.mall.utils.MyBeanUtils;
import com.clever.mall.utils.StatusCode;
import com.clever.mall.vo.AddressVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: project-item
 * @author: hzx
 * @since: JDK 1.8
 * @create: 2020-11-10 17:28
 **/

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper mapper;

    /**
     * 通过用户id和地址id获取地址信息
     * @param aid
     * @return
     */
    @Override
    public AddressVo selectAddressById(int aid) {
        if (aid < 1) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }

        //查询数据
        Address address = mapper.selectAddressById(aid);
        AddressVo addressVo = new AddressVo();
        MyBeanUtils.copyProperties(address,addressVo);

        if (addressVo == null) {
            throw new ServiceException(StatusCode.NO_DATA_ERROR);
        }

        return addressVo;
    }

    /**
     * 通过用户id获取所有用户地址信息
     * @param uid
     * @return
     */
    @Override
    public List<AddressVo> selectAddressAll(int uid) {
        if (uid < 1) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }

        //查询数据
        List<Address> addressList = mapper.selectAddressAll(uid);
        List<AddressVo> addressVos = MyBeanUtils.copyListProperties(addressList, AddressVo::new);

        if (addressVos == null || addressList.size() == 0) {
            throw new ServiceException(StatusCode.NO_DATA_ERROR);
        }

        return addressVos;
    }

    /**
     * 添加地址
     * @param addressDto
     * @return
     */
    @Override
    @Transactional
    public int insertAddress(AddressDto addressDto) {
        if (addressDto == null) {
            throw new ServiceException(StatusCode.NULL_ERROR);
        }
        /*if (IsClassNull.isNotEmptyBean(addressDto)) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }*/

        //设置为默认地址时将所有该用户的地址设置为非默认
        if (addressDto.getIsDefault() == 1) {
            if (mapper.updateAddressIsDefault(addressDto.getUserId()) < 1) {
                throw new ServiceException(StatusCode.ADD_DATABASE_DATA_ERROR);
            }
        }

        //复制数据
        Address address = new Address();
        MyBeanUtils.copyProperties(addressDto,address);

        //录入数据库
        int insertAddress = mapper.insertAddress(address);

        if (insertAddress < 1) {
            throw new ServiceException(StatusCode.ADD_DATABASE_DATA_ERROR);
        }

        return insertAddress;
    }

    /**
     * 修改地址
     * @param addressDto
     * @return
     */
    @Override
    public int updateAddress(AddressDto addressDto) {
        if (addressDto == null) {
            throw new ServiceException(StatusCode.NULL_ERROR);
        }
        if (IsClassNull.isNotEmptyBean(addressDto)) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }

        //设置为默认地址时将所有该用户的地址设置为非默认
        if (addressDto.getIsDefault() == 1) {
            if (mapper.updateAddressIsDefault(addressDto.getUserId()) < 1) {
                throw new ServiceException(StatusCode.ADD_DATABASE_DATA_ERROR);
            }
        }

        //复制数据
        Address address = new Address();
        MyBeanUtils.copyProperties(addressDto,address);

        //调用数据库修改数据
        int updateAddress = mapper.updateAddress(address);

        if (updateAddress < 1) {
            throw new ServiceException(StatusCode.ADD_DATABASE_DATA_ERROR);
        }

        return updateAddress;
    }

    /**
     * 删除地址
     * @param uid
     * @param aid
     * @return
     */
    @Override
    public int delAddress(int uid, int aid) {
        if (uid < 1 || aid < 1) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }

        //删除
        int delAddressSize = mapper.delAddress(uid, aid);

        if (delAddressSize < 1) {
            throw new ServiceException(StatusCode.ERROR);
        }

        return delAddressSize;
    }
}
